<?php

class mcategories extends CI_Model{
    
    private $tbl = "categories";
    
    public function fetchAll(){
        return $this->db->get($this->tbl)->result();
    }
    
    public function fetchOne($id){
        return $this->db->where('id',$id)->get($this->tbl)->row();
    }
    
    public function insert($arr = array()){
        $this->db->insert($this->tbl,$arr);
    }
    
    public function update($id,$arr = array()){
        $this->db->where('id',$id)->update($this->tbl,$arr);
    }
    
    public function delete($id){
        $this->db->delete($this->tbl, array('id' => $id)); 
    }
    
    public function deleteByParentId($id){
        $this->db->delete($this->tbl, array('pid' => $id)); 
    }
    
    public function deleteAll(){
        $this->db->empty_table($this->tbl); 
    }
    
    
    public function getParentCat(){
        return $this->db->where('pid',0)->get($this->tbl)->result();
    }
    
    public function getChildCat($id){
        return $this->db->where('pid',$id)->get($this->tbl)->result();
    }
    
    public function getParentId($id){
        return $this->db->select('pid')->where('id',$id)->get($this->tbl)->row();      
    }
}
